package com.tools.web.work.captcha;

public interface CaptchaRedisLuaScript {

    String CHECK_EQUALS =
            "local key = KEYS[1]" +
            "local sourceCode = ARGV[1]" +
            "local ignoreCase = ARGV[2] == 'true'" +
            "-- 获取 captchaBean 表" +
            "local captchaBean = redis.call('HGETALL', key)" +
            "if #captchaBean == 0 then" +
            "   return 0" +
            "end" +
            "-- 将 captchaBean 表转为一个 Lua table" +
            "local beanTable = {}" +
            "for i = 1, #captchaBean, 2 do" +
            "   beanTable[captchaBean[i]] = captchaBean[i + 1]" +
            "end" +
            "-- 获取表中的 captcha 属性" +
            "local captcha = beanTable['captcha']" +
            "if not captcha then" +
            "   return 0" +
            "end" +
            "-- 忽略大小写或区分大小写比较" +
            "local isMatch" +
            "if ignoreCase then" +
            "   isMatch = string.lower(captcha) == string.lower(sourceCode)" +
            "else" +
            "   isMatch = captcha == sourceCode" +
            "end" +
            "if isMatch then" +
            "   -- 匹配成功，删除 key，返回 1" +
            "   redis.call('DEL', key)" +
            "   return 1" +
            "else" +
            "    -- 匹配失败，获取 TTL" +
            "   local ttl = redis.call('TTL', key)" +
            "   if ttl <= 0 then" +
            "       return 0" +
            "   end" +
            "   -- 获取 allow 和 incorrect 属性，并转为数字" +
            "   local allow = tonumber(beanTable['allow'] or 0)" +
            "   local incorrect = tonumber(beanTable['incorrect'] or 0)" +
            "    -- 自增 incorrect 属性" +
            "   incorrect = incorrect + 1" +
            "   redis.call('HSET', key, 'incorrect', incorrect)" +
            "   -- 更新 key 的过期时间" +
            "   redis.call('EXPIRE', key, ttl)" +
            "   -- 判断 incorrect 是否大于等于 allow" +
            "   if incorrect >= allow then" +
            "       return -2" +
            "   else" +
            "       return -1" +
            "   end" +
            "end";



    String CHECK_SLIDE =
            "local key = KEYS[1]" +
            "local srcBlockX = tonumber(ARGV[1])" +
            "local srcBlockY = tonumber(ARGV[2])" +
            "-- 获取 captchaBean 表" +
            "local captchaBean = redis.call('HGETALL', key)" +
            "if #captchaBean == 0 then" +
            "   return 0" +
            "end" +
            "-- 将 captchaBean 表转为一个 Lua table" +
            "local beanTable = {}" +
            "for i = 1, #captchaBean, 2 do" +
            "   beanTable[captchaBean[i]] = captchaBean[i + 1]" +
            "end" +
            "-- 从 beanTable 中获取 blockX、blockY、faultTolerant，并转为数字" +
            "local blockX = tonumber(beanTable['blockX'] or 0)" +
            "local blockY = tonumber(beanTable['blockY'] or 0)" +
            "local faultTolerant = tonumber(beanTable['faultTolerant'] or 5)" +
            "-- 判断逻辑" +
            "local isMatch = math.abs(blockX - srcBlockX) <= faultTolerant and math.abs(blockY - srcBlockY) <= faultTolerant" +
            "if isMatch then" +
            "   -- 匹配成功，删除 key，返回 1" +
            "   redis.call('DEL', key)" +
            "   return 1" +
            "else" +
            "    -- 匹配失败，获取 TTL" +
            "   local ttl = redis.call('TTL', key)" +
            "   if ttl <= 0 then" +
            "       return 0" +
            "   end" +
            "   -- 获取 allow 和 incorrect 属性，并转为数字" +
            "   local allow = tonumber(beanTable['allow'] or 0)" +
            "   local incorrect = tonumber(beanTable['incorrect'] or 0)" +
            "    -- 自增 incorrect 属性" +
            "   incorrect = incorrect + 1" +
            "   redis.call('HSET', key, 'incorrect', incorrect)" +
            "   -- 更新 key 的过期时间" +
            "   redis.call('EXPIRE', key, ttl)" +
            "   -- 判断 incorrect 是否大于等于 allow" +
            "   if incorrect >= allow then" +
            "       return -2" +
            "   else" +
            "       return -1" +
            "   end" +
            "end";


    String CHECK_CLICK = 
            "local cjson = require 'cjson'" +
            "local chars = cjson.decode(ARGV[1])" +
            "-- 从 Redis 中获取原数据" +
            "local key = KEYS[1]" +
            "local t = redis.call('HGETALL', key)" +
            "if #t == 0 then" +
            "   return 0" +
            "end" +
            "local beanTable = {}" +
            "for i = 1, #t, 2 do" +
            "   beanTable[t[i]] = t[i + 1]" +
            "end" +
            "local sourceChars = cjson.decode(beanTable['captcha'])" +
            "-- 匹配结果" +
            "local isMatch = true" +
            "local minLength = math.min(#chars, #sourceChars)" +
            "for i = 1, minLength do" +
            "   local src = sourceChars[i]" +
            "   local tar = chars[i]" +
            "   local res = (src.word ~= tar.word) or (math.abs(src.x - tar.x) > src.faultTolerant)" +
            "                   or (math.abs(src.y - tar.y) > src.faultTolerant)" +
            "   if res then" +
            "       isMatch = false" +
            "       break" +
            "   end" +
            "end" +
            "if isMatch then" +
            "   -- 匹配成功，删除 key，返回 1" +
            "   redis.call('DEL', key)" +
            "   return 1" +
            "else" +
            "    -- 匹配失败，获取 TTL" +
            "   local ttl = redis.call('TTL', key)" +
            "   if ttl <= 0 then" +
            "       return 0" +
            "   end" +
            "   -- 获取 allow 和 incorrect 属性，并转为数字" +
            "   local allow = tonumber(beanTable['allow'] or 0)" +
            "   local incorrect = tonumber(beanTable['incorrect'] or 0)" +
            "    -- 自增 incorrect 属性" +
            "   incorrect = incorrect + 1" +
            "   redis.call('HSET', key, 'incorrect', incorrect)" +
            "   -- 更新 key 的过期时间" +
            "   redis.call('EXPIRE', key, ttl)" +
            "   -- 判断 incorrect 是否大于等于 allow" +
            "   if incorrect >= allow then" +
            "       return -2" +
            "   else" +
            "       return -1" +
            "   end" +
            "end";


    String SET_EQ_AND_CLICK_CAPTCHA =
            "local key = KEYS[1]" +
            "local expiration = ARGV[1]" +
            "local captcha = ARGV[2]" +
            "local allow = ARGV[3]" +
            "local incorrect = ARGV[4]" +
            "redis.call('HSET', key, 'captcha', captcha, 'allow', allow, 'incorrect', incorrect)" +
            "redis.call('EXPIRE', key, expiration)" +
            "return 1";

    String SET_SLIDE_CAPTCHA =
            "local key = KEYS[1]" +
            "local expiration = ARGV[1]" +
            "local blockX = ARGV[2]" +
            "local blockY = ARGV[3]" +
            "local faultTolerant = ARGV[4]" +
            "local allow = ARGV[5]" +
            "local incorrect = ARGV[6]" +
            "redis.call('HSET', key, 'blockX', blockX, 'blockY', blockY, 'faultTolerant', faultTolerant, 'allow', allow, 'incorrect', incorrect)" +
            "redis.call('EXPIRE', key, expiration)" +
            "return 1";
}
